#!/bin/sh

# Copyright (c) 2022 Simon Josefsson
# Copyright (c) 2023 Daiki Ueno
# License: GPLv3+ <http://gnu.org/licenses/gpl.html>

: ${CLANG_FORMAT=clang-format}

if ! $CLANG_FORMAT --version 2>&1 >/dev/null; then
    echo 1>&2 "$0: clang-format is missing"
    exit 77
fi

parallel=0
mode=indent

while test $# -gt 0; do
    case "$1" in
	--parallel | -p )
	    shift
            parallel=$1
            shift ;;
	--check )
	    mode=check
	    shift ;;
	--help )
	    echo "Usage: indent-gnutls [--parallel=MAX-PROCS] [--check]"
            exit ;;
	-* )
            echo "indent-gnutls: unknown option $1" 1>&2
            echo "Try 'indent-gnutls --help' for more information." 1>&2
            exit 1 ;;
	* )
            break ;;
    esac
done

case $mode in
    check )
	CLANG_FORMAT_ARGS="$CLANG_FORMAT_ARGS --dry-run -Werror"
	;;
    indent )
	CLANG_FORMAT_ARGS="$CLANG_FORMAT_ARGS -i"
	;;
esac

git ls-files -z | grep -z '\.[ch]\(.in\)\?$' | grep -z -v '^devel/' | grep -z -v '^lib/dlwrap/' | grep -z -v '^lib/crau/' | xargs -P "$parallel" -0 -n1 "$CLANG_FORMAT" $CLANG_FORMAT_ARGS

exit $?
